package com.example.petproject.service.impl;

import com.example.petproject.entity.model.PmsSku;
import com.example.petproject.entity.vo.CartProductVO;
import com.example.petproject.entity.vo.PreSubmitProductVO;
import com.example.petproject.entity.vo.PreSubmitVO;
import com.example.petproject.mapper.PmsSkuMapper;
import com.example.petproject.service.OmsOrderService;
import com.example.petproject.service.third.ali.AliYunOSSService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 包名称：com.example.petproject.service.impl
 * 类名称：OmsOrderServiceImpl
 * 类描述：订单service层的实现
 * 创建人：@author DrinkMore-HotWater
 * 创建时间：2024年01月24日 10:06
 **/
@Service
public class OmsOrderServiceImpl implements OmsOrderService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private PmsSkuMapper pmsSkuMapper;
    @Resource
    private AliYunOSSService aliYunOSSService;

    @Override
    public PreSubmitVO preSubmit(Map<String, List<Map<String, Long>>> preSubmitParam,Long userId) {
        //先取出参数
        List<Map<String, Long>> skuInfos = preSubmitParam.get("skuInfos");
        /*{skuInfos: [{skuId: 1000001, skuNum: 3}]}*/
        List<PreSubmitProductVO> preSubmitProductList = skuInfos.stream().map(
                //每一个skuInfo就是一个Map {skuId: 1000001, skuNum: 3}
                skuInfo -> {
                    Long skuId = skuInfo.get("skuId");
                    Long skuNum = skuInfo.get("skuNum");
                    //从数据库中取出对应的商品信息
                    CartProductVO cartProductVO = pmsSkuMapper.selectCartProductVOBYskuId(skuId);
                    PreSubmitProductVO preSubmitProductVO = new PreSubmitProductVO();
                    preSubmitProductVO.setId(skuId);
                    preSubmitProductVO.setNum(Integer.valueOf(String.valueOf(skuNum)));
                    preSubmitProductVO.setTitle(cartProductVO.getTitle());
                    //商品的单价
                    preSubmitProductVO.setUnitPrice(cartProductVO.getUnitPrice());
                    //商品图片,要处理为临时访问路径
                    preSubmitProductVO.setPicUrl(aliYunOSSService.getSymink(cartProductVO.getPicUrl()));
                    return preSubmitProductVO;
                }
        ).toList();
        //计算商品总金额
        BigDecimal totalMoney = BigDecimal.ZERO;
        //通过循环累加每一件商品的总价
        for (PreSubmitProductVO preSubmitProductVO : preSubmitProductList) {
            Integer num = preSubmitProductVO.getNum();
            BigDecimal unitPrice = preSubmitProductVO.getUnitPrice();
            //计算每一个商品总价，将所有的商品总价累计，得到整个预提交商品的总价
            totalMoney = totalMoney.add(unitPrice.multiply(BigDecimal.valueOf(num)));
        }
        //计算代付款金额 = 商品的总金额+运费
        BigDecimal payMoney = totalMoney.add(BigDecimal.TEN);
        PreSubmitVO preSubmitVO = new PreSubmitVO();
        preSubmitVO.setFreight(BigDecimal.TEN);
        preSubmitVO.setPayMoney(payMoney);
        preSubmitVO.setProductList(preSubmitProductList);
        preSubmitVO.setTotalMoney(totalMoney);
        return preSubmitVO;
    }
}
